package org.zcj.util;

public class PageUtil {

	//根据当前页和总记录数创建page对象
	public static Page createPage(int size, int currentpage, int totalcounts){
		int totalpages = totalPages(size, totalcounts);
		int prepage  = prePage(currentpage);
		int nextpage = nextPage(currentpage, totalpages);
		int beginindex = beginIndex(size,currentpage);
		int lastindex = lastIndex(beginindex, size);
		return new Page(currentpage, size, totalcounts, totalpages, prepage, nextpage, beginindex, lastindex);
	}
	
	//停止搜索的地方
	private static int lastIndex(int beginindex, int size){
		int lastindex = beginindex + size;
		return lastindex;
	}
	
	//返回开始查询的地方
	private static int beginIndex(int size, int currentpage){
		int beginindex = (currentpage - 1) * size;
		return beginindex;
	}
	
	//下一页
	private static int nextPage(int currentpage, int totalpages){
		int nextpage = (currentpage == totalpages)?totalpages:(currentpage + 1);
		return nextpage;
	}
	
	//上一页
	private static int prePage(int currentpage){
		int prepage = (currentpage == 1)?1:(currentpage - 1);
		return prepage;
	}
	
	//总过几页
	private static int totalPages(int size, int totalcounts){
		if(totalcounts == 0){
			return 0;
		}
		if(totalcounts % size == 0){
			return totalcounts/size;
		}else {
			return totalcounts/size+1;
		}
	}
}
